<template>
  <div class="login-wrap">
    <header class="login-top">
      <img src="../../assets/images/logo.png" alt="" />
    </header>

    <main>
      <h3>登录</h3>
      <el-form
        :model="form"
        label-width="auto"
        :rules="rules"
        ref="ruleFormRef"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            :prefix-icon="Lock"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="form.checked" label="记住密码" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 200px"
            @click="login(ruleFormRef)"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </main>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";

// import axios from "axios";
// import { post } from "@/assets/js/request";
import { loginApi } from "@/assets/js/api";

interface RuleForm {
  username: string;
  password: string;
}

// 路由实例，用于做跳转
const router = useRouter();
// form表单的字段
const form = reactive({
  username: localStorage.getItem("username") || "",
  password: localStorage.getItem("password") || "",
  checked: !!localStorage.getItem("checked") || false,
});
// 校验规则
const rules = reactive<FormRules<RuleForm>>({
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
    { min: 3, max: 8, message: "用户名长度必须是3-8位", trigger: "blur" },
    // {
    //   type: "enum",
    //   enum: ["admin", "user", "guest"],
    //   message: "用户名错误",
    //   trigger: "blur",
    // },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
    // {
    //   type: "email",
    //   message: "请输入正确的邮箱",
    // },
  ],
});
// 用于获取到form表单元素
const ruleFormRef = ref<FormInstance>();

// 登录函数
const login = (formEl: FormInstance | undefined): void => {
  if (!formEl) return;
  // 先做校验
  formEl.validate((valid, fields) => {
    if (valid) {
      // 校验完成
      // 就可以在这里做数据请求

      loginApi({
        username: form.username,
        password: form.password,
      }).then((res) => {
        if (form.checked) {
          localStorage.setItem("username", form.username);
          localStorage.setItem("password", form.password);
          localStorage.setItem("checked", form.checked + "");
        } else {
          localStorage.clear();
        }
        localStorage.setItem("token", res.token);
        router.push("/home");
      });
      // axios
      //   .post("http://101.35.148.193:3001/api/admin/login", {
      //     username: form.username,
      //     password: form.password,
      //   })
      //   .then((res) => {
      //     if (res.data.success) {
      //       if (form.checked) {
      //         localStorage.setItem("username", form.username);
      //         localStorage.setItem("password", form.password);
      //         localStorage.setItem("checked", form.checked + "");
      //       } else {
      //         localStorage.clear();
      //       }
      //       localStorage.setItem("token", res.data.token);
      //       router.push("/home");
      //     } else {
      //       ElMessage.error(res.data.msg);
      //     }
      //   })
      //   .catch((error) => {
      //     console.log(error);
      //   });
    } else {
      // 消息提示框
      // 需要在main.ts里面引入element的css
      ElMessage.error("用户名或者密码错误");
    }
  });
};
</script>

<style scoped lang="less">
.login-wrap {
  width: 100vw;
  height: 100vh;
  background: url("../../assets/images/background.jpg") no-repeat center center /
    cover;

  .login-top {
    width: 100%;
    height: 70px;
    padding: 10px;
    background-color: rgba(255, 255, 255, 0.4);

    img {
      height: 50px;
    }
  }

  main {
    padding: 20px;
    width: 400px;
    height: 300px;
    background-color: #fff;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateY(-50%);
    border-radius: 6px;
    background-color: rgba(255, 255, 255, 0.7);

    h3 {
      text-align: center;
      font-size: 20px;
      margin: 20px 0;
    }
  }
}
</style>
